package com.hmdp.service.impl;

import com.hmdp.domain.dto.Result;
import com.hmdp.domain.dto.UserDTO;
import com.hmdp.domain.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hmdp.utils.RedisConstants.FOLLOW_BLOG_USER_ID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    FollowMapper followMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 取关和关注
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();

        return Result.ok();

    }

    /**
     * 查看是否关注
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        Double score = stringRedisTemplate.opsForZSet().score(FOLLOW_BLOG_USER_ID+followUserId,userId);
        if (score==null){
            return Result.ok(Boolean.FALSE);
        }
        return Result.ok(Boolean.TRUE);
    }

    @Override
    public Result followCommons(Long id) {
        Long userId = UserHolder.getUser().getId();
        List<UserDTO> userDTOS = followMapper.followCommons(userId, id);
        return Result.ok(userDTOS);
    }
}
